package com.bestom.companylights;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

import android_serialport_api.SerialPort;

public class InitApplication extends Application {

    public static InitApplication application;
    private SharedPreferences mSharedPreferences;
    public SerialPort mSerialPort;
    public String sharedPreferencesName;
    public static InitApplication getApplication(){
        return application;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        openSerialPort();
        mSharedPreferences = getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
    }

    private void openSerialPort() {
        try {
            mSerialPort = new SerialPort(new File("/dev/ttyS3"), 115200, 0);

        } catch (IOException e) {
            Log.d("SerialPortService", "找不到该设备文件");
        }
    }

    public InputStream getmInputStream(){
        if (mSerialPort == null) return null;
        return mSerialPort.getInputStream();
    }
    public OutputStream getmOutputStream(){
        if (mSerialPort == null) return null;
        return mSerialPort.getOutputStream();
    }

    public void put(String key, Object value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else  if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else if (value instanceof Set) {
            Set set = (Set) value;
            Set<String> newSet = new HashSet<>();
            for (Object object : set) {
                newSet.add((String)object);
            }
            editor.putStringSet(key, newSet);
        } else {
            throw new IllegalArgumentException("Unsupported value type");
        }

        editor.apply();
    }

    public Object get(String key, Object defaultValue) {
        if (defaultValue instanceof String) {
            return mSharedPreferences.getString(key, (String) defaultValue);
        } else if (defaultValue instanceof Boolean) {
            return mSharedPreferences.getBoolean(key, (Boolean) defaultValue);
        } else if (defaultValue instanceof Float) {
            return mSharedPreferences.getFloat(key, (Float) defaultValue);
        } else if (defaultValue instanceof Integer) {
            return mSharedPreferences.getInt(key, (Integer) defaultValue);
        } else if (defaultValue instanceof Long) {
            return mSharedPreferences.getLong(key, (Long) defaultValue);
        } else if (defaultValue instanceof Set) {
            //noinspection unchecked
            return mSharedPreferences.getStringSet(key, (Set<String>) defaultValue);
        } else {
            return null;
        }
    }

}
